package com.zmx.doctor.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.DepartmentRightApadter;
import com.zmx.doctor.adapter.DoctorTitleAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.AuthenticationInformation;
import com.zmx.doctor.pojo.DoctorTitle;
import com.zmx.doctor.pojo.HospMessage;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*时间：2020/7/4 20:26
*功能：医生称号
*/
public class DoctorTitleActivity extends BaseActivity {

    private RecyclerView reView;
    private DoctorTitleAdapter adapter;
    private List<DoctorTitle> lists;
    private AuthenticationInformation ai;

    @Override
    protected int getLayoutId(){
        return R.layout.activity_doctor_title;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        ai = (AuthenticationInformation) getIntent().getSerializableExtra("ai");

        reView = findViewById(R.id.reView);
        lists = new ArrayList<>();
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(4,
                        StaggeredGridLayoutManager.VERTICAL);
        reView.setLayoutManager(staggeredGridLayoutManager);
        adapter = new DoctorTitleAdapter(this);
        reView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                ai.setTitle_id(lists.get(listPosition).getId()+"");
                handler.sendEmptyMessage(2);

            }
        });

        getAllDoctor();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    Log.e("数据","数据"+msg.obj.toString());

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i=0;i<array.length();i++){

                            DoctorTitle hm = gson.fromJson(array.getJSONObject(i).toString(),DoctorTitle.class);
                            lists.add(hm);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);//就是设置list然后刷新

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 2:
                    getData();
                    break;

                case 3:

                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());

                        Toast(object.getString("msg"));
                        if(object.getInt("code")==1){
                            finish();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void getAllDoctor() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getTitle");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app", params, handler, 1, 404);

    }

    public void getData() {
        List<String> ls = new ArrayList<>();
        ls.add(ai.getCard());
        ls.add(ai.getCardside());
        ls.add(ai.getClass_id());
        ls.add(ai.getHosp_id());
        ls.add(ai.getIdcard());
        ls.add(ai.getName());
        ls.add(ai.getOpenprove());
        ls.add(ai.getProce());
        ls.add(ai.getTitle_id());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "checkAuth");
        params.put("data[card]", ai.getCard());
        params.put("data[cardside]", ai.getCardside());
        params.put("data[class_id]", ai.getClass_id());
        params.put("data[hosp_id]", ai.getHosp_id());
        params.put("data[idcard]", ai.getIdcard());
        params.put("data[name]", ai.getName());
        params.put("data[openprove]", ai.getOpenprove());
        params.put("data[prove]", ai.getProce());
        params.put("data[title_id]", ai.getTitle_id());
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app", params, handler, 3, 404);

    }

}
